#!/bin/sh

PREREQ=""

# Duplicated in features/step_definitions/erase_memory.rb
KERNEL_MEM_RESERVED_K=$((64*1024))
ADMIN_MEM_RESERVED_K=$((128*1024))

prereqs() {
   echo "${PREREQ}"
}

tweak_sysctl() {
   echo 3   > /proc/sys/kernel/printk

   echo 3   > /proc/sys/vm/drop_caches

   echo 0   > /proc/sys/vm/overcommit_memory
   echo 0   > /proc/sys/vm/oom_kill_allocating_task
   echo 0   > /proc/sys/vm/oom_dump_tasks

   if [ "${sdmemdebug}" = 1 ] ; then
    # Make sure the kernel doesn't starve, for better reliability when
    # running the test suite.
    echo "$KERNEL_MEM_RESERVED_K" > /proc/sys/vm/min_free_kbytes
    echo "$ADMIN_MEM_RESERVED_K"  > /proc/sys/vm/admin_reserve_kbytes
   fi
}

case ${1} in
   prereqs)
      prereqs
      exit 0
      ;;
esac

if [ -n "${sdmem}" ] ; then
   tweak_sysctl
   if [ -z "${sdmemopts}" ] ; then
      sdmemopts="v"
   fi
   # We run one instance of sdmem per 2 GiB of RAM to deal with the
   # x86 per-process memory limitation.
   mem=$(free | awk '/Mem:/ { print $2 }')
   instances=$((${mem}/(2*1024*1024)+1))
   for i in $(seq 1 "${instances}") ; do
      ( /usr/bin/sdmem "-${sdmemopts}" ; killall sdmem >/dev/null 2>&1 ) &
   done
   # Wait for sdmem jobs to finish.
   wait
fi

if [ "${sdmemdebug}" = 1 ] ; then
   clear
   echo "Going to sleep 10 minutes. Happy dumping!"
   sleep 600
fi

case "${sdmem}" in
   halt)
      /sbin/poweroff -fd
      ;;
   reboot)
      /sbin/reboot -fd
      ;;
   *)
      ;;
esac
